﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace learncoremvc.EF
{
    /// <summary>
    /// 系统上下文
    /// </summary>
    public class LightContext : DbContext
    {
        public bool? IsDeleted { get; private set; } //禁止外界对IsDeleted进行赋值操作，限制在构造函数赋值

        public LightContext(DbContextOptions<LightContext> options, bool? isDeleted = false) : base(options)
        {
            IsDeleted = isDeleted;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ApplicationUser>(m =>
            {
                m.Property(t => t.Email)
                        .HasMaxLength(50);
                m.HasKey(t => t.UID);
                    
                m.Property(t => t.UserName)
                        .IsRequired()
                        .HasMaxLength(50);

                m.Property(t => t.Password)
                        .IsRequired()
                        .HasMaxLength(20);

                m.HasQueryFilter(n => !n.IsDeleted); //默认查询未删除的用户
            });
            base.OnModelCreating(modelBuilder);
        }

        /// <summary>
        /// 系统应用用户
        /// </summary>
        public DbSet<ApplicationUser> ApplicationUser { get; set; }
    }
    public class ApplicationUser
    {
        public string UID { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string CreateDate { get; set; }
        public string CreateBy { get; set; }
        public bool IsDeleted { get; set; }
    }

}
